package com.xb.loan.cashier.biz.service.impl;

import com.xb.loan.cashier.biz.dao.CashierAccountDao;
import com.xb.loan.cashier.biz.dao.CashierAccountDetailDao;
import com.xb.loan.cashier.biz.dao.bean.CashierAccount;
import com.xb.loan.cashier.biz.dao.bean.CashierAccountDetail;
import com.xb.loan.cashier.biz.service.CashierAccountService;
import com.xb.loan.util.cache.redis.comm.FormatOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Service("cashierAccountService")
public class CashierAccountServiceImpl implements CashierAccountService {

    @Autowired
    private CashierAccountDao cashierAccountDao;
    @Autowired
    private CashierAccountDetailDao cashierAccountDetailDao;
    
    @Override
    public void insertAccount(String accountId, int payChannelType, int orderType, String accountDay) {
        CashierAccount cashierAccount = new CashierAccount();
        cashierAccount.setPaychannelType(payChannelType);
        cashierAccount.setOrderType(orderType);
        cashierAccount.setAccountDay(accountDay);
        cashierAccount.setCashierAccountId(accountId);
        cashierAccountDao.insertAccount(cashierAccount);
    }

    @Override
    public String selectAccountId() {
        return FormatOperation.assemblySeq("cashierAccountId", 4, FormatOperation.formatDateToYMDHms(new Date()), "");
    }

    @Override
    public void insertAccountNon(int payChannelType, int orderType) {
        CashierAccount cashierAccount = new CashierAccount();
        cashierAccount.setPaychannelType(payChannelType);
        cashierAccount.setOrderType(orderType);
        cashierAccount.setAccountDay(NOT_ACCOUNT_DAY);
        cashierAccount.setCashierAccountId(selectAccountId());
        cashierAccountDao.insertAccountNon(cashierAccount);
    }

    @Override
    public CashierAccount selectAccountNon(int payChannelType, int orderType) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("paychannelType", payChannelType);
        params.put("orderType", orderType);
        params.put("day", NOT_ACCOUNT_DAY);
        return cashierAccountDao.selectAccountByDay(params);
    }

    @Override
    public void insertAccountDetail(CashierAccountDetail accountDetail) {
        cashierAccountDetailDao.insert(accountDetail);
    }

    @Override
    public int updateAccount(CashierAccount cashierAccount) {
        return cashierAccountDao.updateAccount(cashierAccount);
    }

    @Override
    public void dealHistoryAccountResult(int payChannelType, int orderType, String day) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("payChannelType", payChannelType);
        params.put("orderType", orderType);
        params.put("day", day);
        cashierAccountDao.deleteUnFinishDetail(params);
        cashierAccountDao.deleteUnFinish(params);
        cashierAccountDao.backupFinish(params);
        cashierAccountDao.backupFinishDetail(params);
        cashierAccountDao.deleteFinish(params);
        cashierAccountDao.deleteFinishDetail(params);
        // 无账期，但account_period为当天的数据
        params.put("noAccountDay", NOT_ACCOUNT_DAY);
        cashierAccountDao.deleteAccountNonDetail(params);
    }

    @Override
    public void deleteAccountDetail(String orderId) {
        cashierAccountDao.deleteAccountDetailByOrderId(orderId);
    }

    @Override
    public int queryCountByAccountPeriod(String accountPeriod, int orderType) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("accountPeriod", accountPeriod);
        params.put("orderType", orderType);
        return cashierAccountDao.queryCountByAccountPeriod(params);
    }

}
